package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.jbm.jbmsupplier.util.ICtrl;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.jbm.jbmsupplier.util.abstracts.SendMessageInterfase.gSocket;

/**
 * Created by 家帮忙 on 2017/2/28.
 */

public class MyNameChang extends ActivityUse {
    @InjectView(R.id.edit_name)
    EditText editName;
    private final int MODINAMNE = 0001;
    @InjectView(R.id.goback_ps_nicheng)
    Button gobackPsNicheng;
    @InjectView(R.id.texui)
    TextView texui;
    @InjectView(R.id.buttbaocunname)
    Button buttbaocunname;
    private SharedPreferences mSharedPreferences;

    @Override
    protected int setContentView1() {
        return R.layout.lz_changname;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        mSharedPreferences = getSharedPreferences("sinopecall",
                Activity.MODE_PRIVATE);
    }

//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        ExitAppUtlis.getInstance().addActivity(this);
//        setContentView(R.layout.lz_changname);
//        ButterKnife.inject(this);
//    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MODINAMNE: {
                    ICtrl modimessage = (ICtrl) msg.obj;
                    if (modimessage.back) {
                        finish();
                    }  else {
                        hint(modimessage.errorinfo);
                    }
                    break;
                }

            }
            super.handleMessage(msg);
        }
    };


    @OnClick({R.id.goback_ps_nicheng, R.id.buttbaocunname})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goback_ps_nicheng:
                finish();
                break;
            case R.id.buttbaocunname:
                if (editName.getText().toString().equals("")) {
                    hint("修改信息不能为空");
                    return;
                }else if(editName.getText().toString().length()>16){
                    hint("昵称太长");
                    return;
                }
                NetWork netWork = new NetWork(handler);
//                if (mSharedPreferences.getInt("result.login_id",0)==0){
                    netWork.setModiname(gSocket.login_id,editName.getText().toString() , MODINAMNE);
//                }else {
//                    netWork.setModiname(mSharedPreferences.getInt("result.login_id", 0),editName.getText().toString() , MODINAMNE);
//                }

                break;
        }
    }
}

